﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using Red.Controls;
using Red.Controls.Shell;
using Red.Controls.ViewModels;
using Red.EngineData.View;

namespace Red.Toolkit.MeshEditor.TreesItems
{
    public class MaterialTreeItemLeaf : TreeItemEngine
    {
        private static Canvas _itemIcon = RedApplication.FindCanvasResource( "appbar_clothes_shirt" );

        public override IEnumerable<RedMenuDataItem> Menu
        {
            get
            {
                List<RedMenuDataItem> res = new List<RedMenuDataItem>()
                {
                    new RedMenuDataItem( new CommandWrapper( MaterialsListCommands.CreateNew, this ) ),
                    new RedMenuDataItem( new CommandWrapper( MaterialsListCommands.Copy, this ) ),
                    new RedMenuDataItem( new CommandWrapper( MaterialsListCommands.Remove, this ) )
                };
                return res;
            }
        }

        public MaterialTreeItemLeaf( MaterialTreeItem parent, string title, string toolTip, DataView view )
            : base( parent, title, toolTip, view, _itemIcon, null, String.Empty, false )
        {
        }

        protected override void OnSynchronized()
        {
            DataView nameView = View.GetSubView( "name" );
            nameView.Refresh( ex => { Title = nameView.Value; } );
        }
    }

    // TreeItemEngine implementation for materials tree
    public class MaterialTreeItem : TreeItemEngine
    {
        private static Canvas _rootIcon = RedApplication.FindCanvasResource( "toilet_paper" );

        public override IEnumerable<RedMenuDataItem> Menu
        {
            get 
            {
                List<RedMenuDataItem> res = new List<RedMenuDataItem>()
                {
                    new RedMenuDataItem( new CommandWrapper( MaterialsListCommands.RemoveUnused, this ) ),
                };
                return res;
            }
        }

        public MaterialTreeItem( MaterialTreeItemRoot parent, string title, string toolTip, DataView view )
            : base( parent, title, toolTip, view, _rootIcon )
        {
        }

        protected override RedTreeItem CreateTreeItem( DataView childView )
        {
            return new MaterialTreeItemLeaf( this, "", "", childView );
        }
    }

    public class MaterialTreeItemRoot : TreeItemEngine
    {
        public MaterialTreeItemRoot( DataView rootView, Action<TreeItemEngine> onSynchronized )
            : base( null, "Materials", "", rootView.GetSubView( "materialsList" ), null, onSynchronized )
        {
        }

        public void Refresh()
        {
            ( Children[0] as TreeItemEngine ).FetchAndRebuild();
        }

        protected override RedTreeItem CreateTreeItem( DataView childView )
        {
            if ( childView.InteropName == "materials" )
            {
                return new MaterialTreeItem( this, "Materials", "Materials", childView );
            }

            return null;
        } 
    }
}
